﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Text.RegularExpressions;

namespace tablegen2.logic
{
    public class TableExcelExportBinary
    {

  
        public static void appendFormatLine(StringBuilder sb, int indent, string fmtstr)
        {

      
                if (string.IsNullOrWhiteSpace(fmtstr.Trim()))
                {
                    fmtstr = Regex.Replace(string.Empty, @"\s", "");
                }
                else
                {
                    fmtstr = Regex.Replace(fmtstr, @"\s", "");
                }
                fmtstr = Regex.Replace(fmtstr, @"\t", "");
            
               // sb.Append(new String('\t', indent));
            //sb.Append(new String('\t', indent * 4));
          
            sb.Append(fmtstr);
            //sb.AppendLine();

        }

        public static void appendFormatLine(StringBuilder sb,string fmtstr)
        {

            if (string.IsNullOrWhiteSpace(fmtstr))
            {
                sb.Append(Regex.Replace(string.Empty, @"\s", ""));
     
            }
            else
            {
                sb.Append(Regex.Replace(fmtstr, @"\s", ""));
            }
        
            sb.Append("\t");
     
       

        }

        public static void appendLine(StringBuilder sb, bool line = true)
        {
            if (line)
                sb.AppendLine();

        }



        public static void exportExcelFile(TableExcelData data, string filePath)
        {
            List<Dictionary<string, object>> lst = data.Rows.Select(a =>
            {
                var r = new Dictionary<string, object>();
                for (int i = 0; i < data.Headers.Count; i++)
                {
                    var hdr = data.Headers[i];
                    var val = a.StrList[i];
                    object obj = null;
                    switch (hdr.FieldType)
                    {
                        case "string":

                            obj = val;
                            break;
                        case "int":
                            {
                                int n = 0;
                                int.TryParse(val, out n);
                                obj = n;
                            }
                            break;
                        case "float":
                            {
                                float n = 0;
                                float.TryParse(val, out n);
                                obj = n;
                            }
                            break;
                        case "long":
                            {
                                long n = 0;
                                long.TryParse(val, out n);
                                obj = n;
                            }
                            break;
                        case "string[]":
                            {
                                string[] strs;
                                var numStrs = val.Split(';');
                                strs = new string[numStrs.Length];


                                for (int j = 0; j < numStrs.Length; j++)
                                {
                                    if (string.IsNullOrEmpty(numStrs[j]))

                                        strs[j] = "";

                                    else
                                        strs[j] = numStrs[j];
                                }

                                obj = strs;

                            }
                            break;
                        case "int[]":
                            {
                                int[] ints;
                                var numStrs = val.Split(';');
                                ints = new int[numStrs.Length];


                                for (int j = 0; j < numStrs.Length; j++)
                                {
                                    if (string.IsNullOrEmpty(numStrs[j]))

                                        ints[j] = 0;

                                    else
                                        ints[j] = Convert.ToInt32(numStrs[j]);
                                }

                                obj = ints;

                            }
                            break;
                        case "long[]":
                            {
                                long[] ints;
                                var numStrs = val.Split(';');
                                ints = new long[numStrs.Length];


                                for (int j = 0; j < numStrs.Length; j++)
                                {
                                    if (string.IsNullOrEmpty(numStrs[j]))

                                        ints[j] = 0;

                                    else
                                        ints[j] = Convert.ToInt64(numStrs[j]);
                                }

                                obj = ints;

                            }
                            break;
                    }
                    r[hdr.FieldName] = obj;
                }
                return r;
            }).ToList();

        

            //创建一个二进制格式化程序
            BinaryFormatter bf = new BinaryFormatter();

            //创建一个文件流来保存
            //Application.dataPath（包含游戏数据文件夹的路径（只读））
            FileStream fileStream = File.Create(filePath);

            //用二进制格式化程序的序列化方法来序列化Save对象。
            //参数：创建的文件流和需要序列化的对象。 
            bf.Serialize(fileStream, lst);

          /*  string output = JsonConvert.SerializeObject(lst, Formatting.Indented);
            File.WriteAllBytes(filePath, Encoding.UTF8.GetBytes(output));*/
        }


    }
    
}
